//import processing.opengl.*;

/*Paul Paddock
 4-16-10
 CSCI340
 Reiser*/

/*This program is meant to allow 7th graders to get used to computing 
 circumference of a circle and relating that to its cylinder's height.
 It also provides chances for the student to experiment with relationships
 between measurements within this medium of cylinder properties.*/

/*It walks through choosing two numbers, and allows the student
 to type in one number as a cylinder's radius, calculate its circle's 
 circumference, then compare that to its height, and discover something
 optically bizarre between the two measurements. Based on a bar trick.*/

//Follow directions on the page
int h, w;
PFont font1, font2, font3, subfont;
PImage img;
Circum eye1;
Height foot;
Setting temp;
Step1 findRadius;

Step3 findHeight;
Final crazy;
float R, T, x, y,circumference;


//loads the necessary files needed and 
//secures any default variable association
void setup() {
  size(900,500,P3D);
  smooth();
  font1=loadFont("DokChampa-25.vlw");
  font2=loadFont("Neurochrome-36.vlw");
  font3=loadFont("Papyrus-Regular-36.vlw");
  subfont=loadFont("AgencyFB-Reg-18.vlw");
  h=height;
  w=width;
}

//run the operations
void draw() {
  background(255,255,0);  
  eye1=new Circum(w/2,R);
  temp=new Setting(50);
  findRadius=new Step1(60);  

  findHeight=new Step3(60);
  foot=new Height(190);
  crazy=new Final(60);
}



//keys correspond to circle radii 2-9 mm
void keyPressed(){
  if (key=='2'){
    R=2.0;
  }
  if (key=='3'){
    R=3.0;
  }
  if (key=='4'){
    R=4.0;
  }
  if (key=='5'){
    R=5.0;
  }
  if (key=='y'){
    T=6.0;
  }
  if (key=='u'){
    T=7.0;
  }
  if (key=='i'){
    T=8.0;
  }
  if (key=='o'){
    T=9.0;
  }
}

//give directions of what to do first and
//display visual for circle with input radius
class Circum {
  Circum(float xPos,float R){
    x=xPos;
    if (key=='2' || key=='3' || key=='4' || key=='5'||key=='6'||key=='7'||key=='8'||key=='9'){
      textFont(subfont, 24);
      text("> Type in a number between 2 & 5.", 60,100);
      text("> Have this be a cylinder's radius.", 60,130);
      textFont(subfont, 24);
      text("Radius = " + R + " mm",w/1.6,h/2);
      eye1.display();
    }
  }
  void display(){
    stroke(1);
    noFill();
    strokeWeight(1);
    ellipse(x, h/2,R*30,R*30);
    textFont(font2);
    text("Press 'b' for next step", 13,h-16);
  }
}
class Height {
  Height(float f){
    if (key=='w' || key=='e' || key=='r' || key=='t'||key=='y'||key=='u'||key=='i'||key=='o'){
      textFont(subfont, 24);
      //Which is bigger?
      stroke(0);
          line(0,h/7,w,h/7);
          noStroke();
      fill(random(180,250),108,random(200,250));
      rect(0,f-70,w,48);
      fill(0);
      textFont(subfont, 24);
      text("> Now compare the height to its circumference. Look at the cylinder.", 60, 105);
      textFont(font1, 30);
      text("Which is bigger?     Circumference... or height?", 60, 150);
      fill(0);
      textFont(subfont, 15);
      text("(move the mouse around)", w-200,f-40);
      textFont(subfont, 24);
      text("Calculate the circumference using 2*pi*radius", width/2-300,h/2+100);
      text("height = " + T + " mm", w/2-300,h/2+30);
      fill(0);
      textFont(font2);
      text("Press 'n'", 13,h-16);
      //cylinder diagram
      fill(0,134,0);
      translate(width / 1.6, height / 2);
      rotateY(map(mouseX, 0, width, 0, PI));
      rotateZ(map(mouseY, 0, height, 0, -PI));
      drawCylinder(R*12, 12*R, T*12, 140);
      translate(-width / 1.6, -height / 2);
    }
  }
}


//display title and how to get into the game
class Setting {
  Setting (int a){
    //How crazy are you?
    fill(0);
    line(0,h/7,w,h/7);
    textFont(font3);
    text("How circum-", a,50);
    textFont(font2);
    text("CRAZY",250,50);
    textFont(font3);
    text("are you?", 440,50);
    textFont(subfont,18);
    text("PRESS 'C' to find out!!!",700, 50);
  }
}

//Pick first number
class Step1 {
  Step1(int b) {
    if (key=='c'){
      textFont(subfont, 24);
      text("> Type in a number between 2 & 5.", 60,100);
      text("> Have this be a cylinder's radius.", 60,130);
    }
  }
}  

//first step
//Calculate circumference manually and check answer through Processing
//next step, pick secong number using keystrokes in diagram
class Step3{
  Step3(int d){
    if (key=='b'){
      stroke(0);
          line(0,h/7,w,h/7);
          noStroke();
      img=loadImage("kb.png");
      image(img,w-270,80);//diagram
      textFont(subfont, 24);
      text("> Now type a number between 6 & 9. (SEE DIAGRAM ON RIGHT)",d,100); 
      text("> Let that be the same cylinder's height.", d, 130);
      //measurements
      fill(0);
      textFont(subfont, 24);
      text("height = " + T + " mm", width/2-300,h/2+30);
      //cylinder diagram
      fill(0,134,0);

      rotateY(map(mouseX, 0, w, 0, PI));
      rotateZ(map(mouseY, 0, h, 0, -PI));
      drawCylinder(R*12, 12*R, T*12, 140);
 
    }

  }
}

//see ending result and reaction
class Final{
  Final(int e){
    if(key=='n'){
    stroke(0);
      line(0,h/7,w,h/7);
      noStroke();
      fill(random(124,135),random(45,156),0);
      textFont(font2, 65);
      text("Weird huh?", e+w/13, h/2);
      fill(0);
      textFont(subfont);
      text("Press 'c' to try this again!!",e+w/5,h/2+ 30);
    }//allows for another go at it

  }
}


//draws the moving cylinder
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
  float angle = 0;
  float angleIncrement = TWO_PI / sides;
  beginShape(QUAD_STRIP);
  for (int i = 0; i < sides + 1; ++i) {
    vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
    vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
    angle += angleIncrement;
  }
  endShape();

  // If it is not a cone, draw the circular top cap
  if (topRadius != 0) {
    stroke(0);
    rotateX(PI/2);
    ellipse(0,0,topRadius*cos(angle)+12*R,12*R+topRadius*cos(angle));
    rotateX(-PI/2);
    noStroke();
    angle = 0;
    beginShape(TRIANGLE_FAN);
    // Center point
    vertex(0, 0, 0);

    for (int i = 0; i < sides + 1; i++) {

      vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
      angle += angleIncrement;
    }
    endShape();
  }

  // If it is not a cone, draw the circular bottom cap
  if (bottomRadius != 0) {
    noStroke();
    angle = 0;
    beginShape(TRIANGLE_FAN);

    // Center point
    vertex(0, tall, 0);
    for (int i = 0; i < sides + 1; i++) {
      vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
      angle += angleIncrement;
    }
    endShape();
  }
}












